const TOKEN_KEY = 'token';

const hasToken = () => {
  return !!localStorage.getItem(TOKEN_KEY);
};

const getToken = () => {
  return localStorage.getItem(TOKEN_KEY);
};

const setToken = (token: string) => {
  localStorage.setItem(TOKEN_KEY, token);
};

const clearToken = () => {
  localStorage.removeItem(TOKEN_KEY);
};

export const goLogin = () => {
  const home = `${window.location.origin}${window.location.pathname}/#/home`;
  window.location.href = `${process.env.LOGIN_RUL}?client_id=${process.env.CLIENT_ID}&response_type=code&redirect_uri=${home}&scope=read&state=casdoor`;
};

export { hasToken, getToken, setToken, clearToken };
